﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Flipmind.Xero;

namespace XeroInvoiceLoaderApp
{
    public partial class EditContactForm : Form
    {

        private Contact contact;
        private XeroAPI api;

        public EditContactForm()
        {
            InitializeComponent();
        }

        public void ShowForm(XeroAPI api)
        {
            this.api = api;
            this.contact = new Contact();
            ShowDialog();
        }

        public void ShowForm(Contact contact)
        {
            this.api = null;
            this.contact = contact;
            ddContactStatus.Items.AddRange(Enum.GetNames(typeof(ContactStatus)));

            txtContactID.Text = contact.ContactID;
            txtContactNumber.Text = contact.ContactNumber;
            ddContactStatus.SelectedItem = contact.ContactStatus.ToString();
            txtEmailAddress.Text = contact.EmailAddress;
            txtContactName.Text = contact.Name;
            txtUpdatedDateUTC.Text = contact.UpdatedDateUTC.ToString();
            if (contact.Addresses != null)
            {
                foreach (ContactAddress add in contact.Addresses)
                {
                    ListViewItem lv = lvAddresses.Items.Add(add.AddressLine1);
                    lv.SubItems.Add(add.AddressLine2);
                    lv.SubItems.Add(add.AddressLine3);
                    lv.SubItems.Add(add.AddressLine4);
                    lv.SubItems.Add(add.AddressType.ToString());
                    lv.SubItems.Add(add.City);
                    lv.SubItems.Add(add.PostalCode);
                    lv.SubItems.Add(add.Region);
                    lv.SubItems.Add(add.Country);
                }
            }
            if (contact.Phones != null)
            {
                foreach (ContactPhone phone in contact.Phones)
                {
                    ListViewItem lv = lvPhones.Items.Add(phone.PhoneCountryCode);
                    lv.SubItems.Add(phone.PhoneAreaCode);
                    lv.SubItems.Add(phone.PhoneNumber);
                    lv.SubItems.Add(phone.PhoneType.ToString());
                }
            }
            ShowDialog();
        }

        private void EditContactForm_Load(object sender, EventArgs e)
        {

        }

        private void btnSave_Click(object sender, EventArgs e)
        {
            contact.ContactID = string.IsNullOrEmpty(txtContactID.Text) ? null : txtContactID.Text;
            contact.ContactNumber = txtContactNumber.Text;
            if (ddContactStatus.SelectedItem != null)
            {
                contact.ContactStatus = (ContactStatus)Enum.Parse(typeof(ContactStatus), (string)ddContactStatus.SelectedItem);
            }
            contact.EmailAddress = txtEmailAddress.Text;
            contact.Name = txtContactName.Text;
            if (!string.IsNullOrEmpty(txtUpdatedDateUTC.Text))
            {
                contact.UpdatedDateUTC = DateTime.Parse(txtUpdatedDateUTC.Text);
            }
            contact.Addresses.Clear();
            
            foreach (ListViewItem li in lvAddresses.Items)
            {
                ContactAddress add = new ContactAddress();
                add.AddressLine1 = li.SubItems[0].Text;
                add.AddressLine2 = li.SubItems[1].Text;
                add.AddressLine3 = li.SubItems[2].Text;
                add.AddressLine4 = li.SubItems[3].Text;
                add.AddressType = (AddressType) Enum.Parse(typeof(AddressType), li.SubItems[4].Text);
                add.City = li.SubItems[5].Text;
                add.PostalCode = li.SubItems[6].Text;
                add.Region = li.SubItems[7].Text;
                add.Country = li.SubItems[8].Text;
                contact.Addresses.Add(add);
            }
            contact.Phones.Clear();
            foreach (ListViewItem li in lvPhones.Items)
            {
                ContactPhone phone = new ContactPhone();
                phone.PhoneCountryCode = li.SubItems[0].Text;
                phone.PhoneAreaCode = li.SubItems[1].Text;
                phone.PhoneNumber = li.SubItems[2].Text;
                phone.PhoneType = (PhoneType)Enum.Parse(typeof(PhoneType), li.SubItems[3].Text);
                contact.Phones.Add(phone);
            }
            if (api != null)
            {
                try
                {
                    api.SaveContact(contact);
                }
                catch (Exception ex)
                {
                    MessageBox.Show("Problem saving contact: " + ex);
                }
            }
            Close();
        }
    }
}

